package com.maimao.search.controller;

import cn.hutool.core.util.ObjectUtil;
import com.maimao.core.enums.ExceptionEnum;
import com.maimao.core.exception.MaiMaoException;
import com.maimao.core.model.charts.OrderAggTimeType;
import com.maimao.core.model.charts.OrderDynamicAggTimeType;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.model.echarts.common.BarLineOption;
import com.maimao.model.echarts.common.ChartsResult;
import com.maimao.model.order.OrderShop;
import com.maimao.model.order.es.EsOrder;
import com.maimao.model.order.input.OrderSearchInput;
import com.maimao.search.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2021/3/29 11:37 下午
 */
@RequestMapping("/order")
@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 分页条件查询订单 【目前只在订单服务——导出功能里调用】
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody OrderSearchInput input, @RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        if (page == null || size == null || page < 0 || size < 0) {
            log.error("分页参数错误");
            throw new MaiMaoException(ExceptionEnum.INVALID_FILE_TYPE);
        }
        if (input.getToTime()==null){
            input.setToTime(DateUtils.now());
        }
        return Result.okData(orderService.list(input, page, size));
    }

    @PostMapping("/saveBatch")
    public Result<?> saveBatch(@RequestBody List<EsOrder> orderList) {
        if (ObjectUtil.isEmpty(orderList)) {
            return Result.notValid("数据为空");
        }
        boolean res = orderService.saveBatch(orderList);
        return Result.okData(res);
    }

    /**
     * 饼图：
     * <p>
     * 维度：进行中(commit,paid)、交易成功、交易取消
     * <p>
     * 时间：当月 三月内 半年内 一年内 去年  累计
     *
     * @param time 时间维度 OrderAggTimeType.code
     */
    @GetMapping("/pieChart")
    public Result<?> pieCharts(@RequestParam(value = "time", required = false) Integer time) {
        if (time == null) {
            time = OrderAggTimeType.ALL.getCode();
        }
        ChartsResult<Map<String, Object>> res = orderService.pieCharts(time);
        return Result.okData(res);
    }

    /*
     动态折线查询/聚合 算法：
            {
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "status": {
                      "value": "5"
                    }
                  }
                },
                {
                  "term": {
                    "createTime": {
                      "value": "xx"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "nestedAgg": { #需先对嵌套对象聚合
              "nested": {
                "path": "orderShopList"
              },
              "aggs": {
                "sumAgg": { # 在nestedAgg的基础上做sum聚合
                  "sum": {
                    "field": "orderShopList.actualPrice"
                  }
                }
              }
            }
          }
        }
     */

    /**
     * 动态折线
     * <p>
     * 维度：柱状：订单总量 订单成交量（交易成功的）无效量； 折线：订单成交总额（所有订单总额去掉取消的订单总额）
     * <p>
     *
     * @return
     */
    @GetMapping("/dynamicChart")
    public Result<?> dynamicCharts(@RequestParam(value = "aggTimeType", required = false) Integer aggTimeType) {
        if (aggTimeType == null) {
            aggTimeType = OrderDynamicAggTimeType.HOUR.getCode();
        }
        BarLineOption<List<Object>> res = orderService.dynamicCharts(aggTimeType);
        return Result.okData(res);
    }
}
